import java.util.HashSet;
import java.util.Random;
import java.util.Set;

// 使用类来表示一个算式结构
class ArithmeticProblem3 {
    int operand1;
    int operand2;
    char operator;
    int result;
    // 构造函数
    public ArithmeticProblem3(int operand1, int operand2, char operator) {
        this.operand1 = operand1;
        this.operand2 = operand2;
        this.operator = operator;
        // 计算结果
        if (operator == '+') {
            this.result = operand1 + operand2;
        } else {
            this.result = operand1 - operand2;
        }
    }

    // 重写toString方法，方便输出（包括答案）
    @Override
    public String toString() {
        return String.format("%2d %c %2d = %3d", operand1, operator, operand2, result);
    }

    // 不带答案的字符串表示
    public String toProblemString() {
        return String.format("%2d %c %2d =   ", operand1, operator, operand2);
    }

    // 重写equals和hashCode方法以支持去重
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        ArithmeticProblem3 that = (ArithmeticProblem3) obj;
        return operand1 == that.operand1 &&
                operand2 == that.operand2 &&
                operator == that.operator;
    }

    @Override
    public int hashCode() {
        return operand1 * 1000 + operand2 * 10 + operator;
    }

    // 获取唯一标识符用于比较
    public String getKey() {
        return operand1 + "" + operator + "" + operand2;
    }
}

public class Calculation_SystemV3 {
    public static void main(String[] args) {
        Random random = new Random();
        Set<String> generatedProblems = new HashSet<>();
        ArithmeticProblem3[] problems = new ArithmeticProblem3[50];
        int count = 0;
        int addCount = 0;
        int subCount = 0;

        System.out.println("欢迎使用加减法口算练习系统！");
        System.out.println("以下是50道100以内的加减法算式：");
        System.out.println("=============================================");

        // 确保至少有加法和减法各一定数量
        // 先生成最少数量的加法题
        while (addCount < 5 && count < 50) {
            int num1 = random.nextInt(101);
            int num2 = random.nextInt(101 - num1);
            String key = num1 + "+" + num2;

            if (!generatedProblems.contains(key)) {
                problems[count] = new ArithmeticProblem3(num1, num2, '+');
                generatedProblems.add(key);
                count++;
                addCount++;
            }
        }

        // 再生成最少数量的减法题
        while (subCount < 5 && count < 50) {
            int num1 = random.nextInt(101);
            int num2 = random.nextInt(num1 + 1);
            String key = num1 + "-" + num2;

            if (!generatedProblems.contains(key)) {
                problems[count] = new ArithmeticProblem3(num1, num2, '-');
                generatedProblems.add(key);
                count++;
                subCount++;
            }
        }

        // 继续生成直到达到50道题，且无重复
        while (count < 50) {
            int num1, num2;
            char operator;
            String key;

            // 随机决定是加法还是减法（0表示加法，1表示减法）
            int operation = random.nextInt(2);

            if (operation == 0) { // 加法
                operator = '+';
                // 确保和不超过100
                num1 = random.nextInt(101);
                num2 = random.nextInt(101 - num1);
                key = num1 + "+" + num2;
            } else { // 减法
                operator = '-';
                // 确保差不小于0
                num1 = random.nextInt(101);
                num2 = random.nextInt(num1 + 1);
                key = num1 + "-" + num2;
            }

            // 检查是否已存在该算式
            if (!generatedProblems.contains(key)) {
                // 创建算式对象
                problems[count] = new ArithmeticProblem3(num1, num2, operator);
                generatedProblems.add(key);
                count++;

                // 更新计数器
                if (operation == 0) {
                    addCount++;
                } else {
                    subCount++;
                }
            }
        }

        // 按照每行5个算式的方式打印（仅显示算式，不显示答案）
        System.out.println("题目:");
        for (int i = 0; i < 50; i += 5) {
            for (int j = 0; j < 5 && (i+j) < 50; j++) {
                System.out.print(String.format("%-15s", problems[i+j].toProblemString()));
            }
            System.out.println();
        }

        System.out.println("\n---------------------------------------------");

        // 按照每行5个算式的方式打印（显示算式和答案）
        System.out.println("题目和答案:");
        for (int i = 0; i < 50; i += 5) {
            for (int j = 0; j < 5 && (i+j) < 50; j++) {
                System.out.print(String.format("%-20s", problems[i+j].toString()));
            }
            System.out.println();
        }

        System.out.println("\n统计信息:");
        System.out.println("加法题数量: " + addCount);
        System.out.println("减法题数量: " + subCount);

        System.out.println("=============================================");
        System.out.println("练习题生成完毕！");
    }
}
